package com.redsoil.house.ui.capital.presenter;

import com.blankj.utilcode.util.LogUtils;
import com.redsoil.house.app.AppContext;
import com.redsoil.house.base.RxPresenter;
import com.redsoil.house.model.DataManager;
import com.redsoil.house.model.bean.CountAssetVO;
import com.redsoil.house.model.bean.DictListVO;
import com.redsoil.house.model.bean.PdfVO;
import com.redsoil.house.model.bean.ReceiptListVO;
import com.redsoil.house.model.bean.ReceiptTotalVO;
import com.redsoil.house.model.http.exception.ApiException;
import com.redsoil.house.model.http.response.HttpResponse;
import com.redsoil.house.ui.capital.contract.CapitalInfoContract;
import com.redsoil.house.utils.RxUtil;
import com.redsoil.house.widget.CommonSubscriber;

import javax.inject.Inject;

import io.reactivex.Flowable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * 作者：weidingqiang on 2017/9/7 15:39
 * 邮箱：dqwei@iflytek.com
 */

public class CapitalInfoPresenter extends RxPresenter<CapitalInfoContract.View> implements CapitalInfoContract.Presenter {

    private DataManager mDataManager;

    @Inject
    public CapitalInfoPresenter(DataManager mDataManager) {
        this.mDataManager = mDataManager;
    }

    @Override
    public void attachView(CapitalInfoContract.View view) {
        super.attachView(view);
        registerEvent();
    }

    private void registerEvent(){

    }

    @Override
    public void postReceiptList() {
        addSubscribe(mDataManager.postReceiptList(AppContext.getInstance().getToken(),AppContext.getInstance().getCurrentMoneyType(),"")
                .compose(RxUtil.<HttpResponse<ReceiptListVO>>rxSchedulerHelper())
                .compose(RxUtil.<ReceiptListVO>handleTestResult())
                .subscribeWith(
                        new CommonSubscriber<ReceiptListVO>(mView) {
                            @Override
                            public void onNext(ReceiptListVO data) {

                                mView.postReceiptListSuccess(data);
                            }

                            @Override
                            public void onError(Throwable e) {
                                //当数据返回为null时 做特殊处理
                                try {
                                    int code = ((ApiException) e).getCode();
                                    mView.responeError(e.getMessage());
                                    return;
                                } catch (Exception ex) {

                                }
                                mView.responeError("数据请求失败，请检查网络！");
                            }

                        }
                )
        );
    }


    @Override
    public void getCountAsset(String type) {
        addSubscribe(mDataManager.countAsset(AppContext.getInstance().getToken(),type)
                .compose(RxUtil.<HttpResponse<CountAssetVO>>rxSchedulerHelper())
                .compose(RxUtil.<CountAssetVO>handleTestResult())
                .subscribeWith(
                        new CommonSubscriber<CountAssetVO>(mView) {
                            @Override
                            public void onNext(CountAssetVO data) {
                                mView.countAssetSuccess(data);
                            }

                            @Override
                            public void onError(Throwable e) {
                                //当数据返回为null时 做特殊处理
                                try {
                                    int code = ((ApiException) e).getCode();
                                    mView.responeError(e.getMessage());
                                    return;
                                } catch (Exception ex) {

                                }
                                mView.responeError("数据请求失败，请检查网络！");
                            }

                        }
                )
        );
    }

    @Override
    public void getMainData() {
        Flowable.concat(
                mDataManager.postReceiptList(AppContext.getInstance().getToken(),AppContext.getInstance().getCurrentMoneyType(),""),
                mDataManager.getDictList(AppContext.getInstance().getToken(),"moneyType"),
                mDataManager.countAsset(AppContext.getInstance().getToken(),AppContext.getInstance().getCurrentMoneyType())
                )

                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeWith(
                        new CommonSubscriber<Object>(mView) {
                            @Override
                            public void onNext(Object data) {
//                                LogUtil.debug("","");
//                                ((HttpResponse) data).data
                                HttpResponse httpResponse = ((HttpResponse) data);


                                if(httpResponse.getData() instanceof ReceiptListVO){
                                    mView.postReceiptListSuccess((ReceiptListVO)(httpResponse.getData()));

                                }else if(httpResponse.getData() instanceof DictListVO){
                                    mView.getDicListSuccess((DictListVO)(httpResponse.getData()));
                                }else if(httpResponse.getData() instanceof CountAssetVO){
                                    mView.countAssetSuccess((CountAssetVO)(httpResponse.getData()));
                                }
                            }

                            @Override
                            public void onError(Throwable e) {
                                //当数据返回为null时 做特殊处理
                                try {
                                    int code = ((ApiException) e).getCode();
                                    mView.responeError(e.getMessage());
                                    return;
                                } catch (Exception ex) {

                                }
                                mView.responeError("数据请求失败，请检查网络！");
                            }

                            @Override
                            public void onComplete() {
                                super.onComplete();
                                mView.getComplete();
                            }
                        }
                );
    }

    @Override
    public void exportEarnings(String exportType, String exportVaule) {
        addSubscribe(mDataManager.exportEarnings(AppContext.getInstance().getToken(),exportType,exportVaule)
                .compose(RxUtil.<HttpResponse<PdfVO>>rxSchedulerHelper())
                .compose(RxUtil.<PdfVO>handleTestResult())
                .subscribeWith(
                        new CommonSubscriber<PdfVO>(mView) {
                            @Override
                            public void onNext(PdfVO data) {

                                mView.exportEarningSuccess(data.getPdf());
                            }

                            @Override
                            public void onError(Throwable e) {
                                //当数据返回为null时 做特殊处理
                                try {
                                    int code = ((ApiException) e).getCode();
                                    mView.responeError(e.getMessage());
                                    return;
                                } catch (Exception ex) {

                                }
                                mView.responeError("数据请求失败，请检查网络！");
                            }

                        }
                )
        );
    }
}
